// Do file that puts together the Swedish data to be used for the paper "Persistent Inequalities"

*Save data on parental occupation from the census for later use

foreach year in 1960 1970 1975 1980 1985 1990 {
use LopNr Ar SEI_joj using "E:\ProjData\SESData\ses-SESData.dta", clear
keep if Ar==`year'
drop if SEI_joj==.
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop Ar occ nocc
rename SEI_joj SEI_joj`year'
tempfile SEI`year'
save `SEI`year''
}

use `SEI1960', clear
foreach year in 1970 1975 1980 1985 1990 {
merge 1:1 LopNr using `SEI`year''
drop _merge
}

tempfile SEI
save `SEI'


*Save data on household for the different elections; use nyko then parish if household is missing in the censuses
use "D:\SCB_ConPol\Stata\FoB\FoB_70.dta", clear
keep LopNr LopNrLgh Forsamling
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
replace LopNrLgh = . if LopNrLgh==0
replace LopNrLgh = Forsamling if LopNrLgh==.
rename LopNrLgh FamId1970
drop Forsamling
tempfile FamId1970
save `FamId1970'

foreach year in 80 85 {
use "D:\SCB_ConPol\Stata\FoB\FoB_`year'.dta", clear
keep LopNr LopNrLgh Forsamling Nyko
destring Nyko, replace force
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
replace LopNrLgh = . if LopNrLgh==0
replace LopNrLgh = Nyko if LopNrLgh==.
replace LopNrLgh = Forsamling if LopNrLgh==.
rename LopNrLgh FamId19`year'
drop Nyko Forsamling
tempfile FamId19`year'
save `FamId19`year''
}

foreach year in 1994 2010 {
use "D:\SCB_ConPol\Stata\LISA\LISA_`year'.dta", clear
keep LopNr FamId
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
drop if FamId==.
rename FamId FamId`year'
tempfile FamId`year'
save `FamId`year''
}



*save info on education on five occasions for later use
use "D:\SCB_ConPol\Stata\FoB\FoB_70.dta", clear
keep LopNr UtbNiva
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen UtbAr1970 = 7 if UtbNiva==1
replace UtbAr1970 = 9 if UtbNiva==2
replace UtbAr1970 = 11 if UtbNiva==3
replace UtbAr1970 = 12 if UtbNiva==4
replace UtbAr1970 = 14 if UtbNiva==5
replace UtbAr1970 = 15.5 if UtbNiva==6
replace UtbAr1970 = 19 if UtbNiva==7
drop UtbNiva
tempfile UtbAr1970
save `UtbAr1970'

foreach year in 1990 2000 2010 2014 {
use "D:\SCB_ConPol\Stata\LISA\LISA_`year'.dta", clear
keep LopNr Sun2000niva
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen UtbAr`year' = 7 if Sun2000niva<200
replace UtbAr`year' = 10 if inrange(Sun2000niva, 200, 299)
replace UtbAr`year' = 9. if Sun2000niva==204
replace UtbAr`year' = 10 if inrange(Sun2000niva, 310, 319)
replace UtbAr`year' = 11 if inrange(Sun2000niva, 320, 329)
replace UtbAr`year' = 12 if inrange(Sun2000niva, 330, 339)
replace UtbAr`year' = 13 if inrange(Sun2000niva, 410, 419)
replace UtbAr`year' = 14 if inrange(Sun2000niva, 520, 529)
replace UtbAr`year' = 15 if inrange(Sun2000niva, 530, 539)
replace UtbAr`year' = 16 if inrange(Sun2000niva, 540, 549)
replace UtbAr`year' = 17 if inrange(Sun2000niva, 550, 559)
replace UtbAr`year' = 18 if inrange(Sun2000niva, 600, 629)
replace UtbAr`year' = 19 if inrange(Sun2000niva, 640, 649)
keep LopNr UtbAr`year'
tempfile UtbAr`year'
save `UtbAr`year''
}



*save info on parish in election years for later use
foreach year in 1970 1982 1994 2010 {
use "D:\SCB_ConPol\Stata\RTB\RTB_`year'.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
keep LopNr Forsamling
gen Lan = floor(Forsamling/10000)
drop if Forsamling==.
rename Forsamling Forsamling`year'
rename Lan Lan`year'
tempfile Fors`year'
save `Fors`year''
}



*Save data on turnout for later use
use "D:\SCB_ConPol\Stata\VD\Valdelt_1970.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen Rost70 = .
replace Rost70 = 0 if r==1
replace Rost70 = 1 if r==2
replace Rost70 = 1 if inrange(r, 4, 6)
keep LopNr Rost70
tempfile Rost70
save `Rost70'

use "D:\SCB_ConPol\Stata\VD\Valdelt_82.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen Rost82 = .
replace Rost82 = 0 if r==1
replace Rost82 = 1 if r==2
replace Rost82 = 1 if inrange(r, 4, 6)
keep LopNr Rost82
tempfile Rost82
save `Rost82'

use "D:\SCB_ConPol\Stata\VD\Valdelt_1994rkl.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen Rost94R = .
replace Rost94R = 0 if r==1
replace Rost94R = 1 if r==2
replace Rost94R = 1 if inrange(r, 4, 6)
keep LopNr Rost94R
tempfile Rost94R
save `Rost94R'

use "D:\SCB_ConPol\Stata\VD\Valdelt_1994eu.dta" , clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen Rost94EU = .
replace Rost94EU = 0 if f==1
replace Rost94EU = 1 if f==2
replace Rost94EU = 1 if inrange(f, 4, 6)
keep LopNr Rost94EU
tempfile Rost94EU
save `Rost94EU'

use "D:\SCB_ConPol\Stata\VD\Valdelt_09.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen Rost09 = .
replace Rost09 = 0 if e==1
replace Rost09 = 1 if e==2
replace Rost09 = 1 if inrange(e, 4, 6)
keep LopNr Rost09
tempfile Rost09
save `Rost09'

use "D:\SCB_ConPol\Stata\VD\Valdelt_2010.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen Rost10 = .
replace Rost10 = 0 if r==1
replace Rost10 = 1 if r==2
replace Rost10 = 1 if inrange(r, 4, 6)
keep LopNr Rost10
tempfile Rost10
save `Rost10'


*Save data on birth and death info for later use
use "D:\SCB_ConPol\Stata\FlerGen\Fodelseuppg.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
keep LopNr FodArMan UtlSvBakG Kon
gen FodAr = floor(FodArMan/100)
tempfile Fodelseuppg
save `Fodelseuppg'

use "D:\SCB_ConPol\Stata\RTB\DodDat.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
gen DodAr = floor(DodDatum/10000)
keep LopNr DodAr
tempfile DodDat
save `DodDat'


*Save data adoptive parents for later use
use "D:\SCB_ConPol\Stata\FlerGen\AdForaldrar.dta", clear
gen occ = 1
bysort LopNr: egen nocc = sum(occ)
keep if nocc==1
drop occ nocc
keep LopNr LopNrAdMor LopNrAdFar
gen Adoption = 1


*********************
***Generate sample***
*********************
tempfile AdForaldrar
save `AdForaldrar'

use "D:\SCB_ConPol\Stata\FlerGen\BioForaldrar.dta", clear
rename lopnrmor LopNrBioMor
rename lopnrfar LopNrBioFar
keep LopNr LopNrBioMor LopNrBioFar

tempfile BioForaldrar
save `BioForaldrar'

merge 1:1 LopNr using `AdForaldrar'
drop _merge


*Gen indicators for native and foreign adoptions
gen AdoptionSv = Adoption
gen AdoptionUtl = Adoption

*Keep those with two identified birth parents in native adoption sample
replace AdoptionSv = . if LopNrBioMor==. | LopNrBioFar==.

*Keep adoptees for whom both the adoptive mother and father are identified in native adoption sample
replace AdoptionSv = . if (LopNrAdMor==. | LopNrAdFar==.)

*Drop those whose who were adopted by birth parents in native adoption sample
replace AdoptionSv = . if LopNrBioMor==LopNrAdMor
replace AdoptionSv = . if LopNrBioFar==LopNrAdFar


merge 1:1 LopNr using `Fodelseuppg'
keep if _merge==3
drop _merge

*Keep only those born abroad by two non-swedish parents in foreign adoption sample
replace AdoptionUtl = . if UtlSvBakG!=11

*Keep only those born with unknown birth parents in foreign adoption sample
replace AdoptionUtl = . if LopNrBioMor!=. | LopNrBioFar!=.

*Keep adoptees for whom both the adoptive mother and father are identified in foreign adoption sample
replace AdoptionUtl = . if (LopNrAdMor==. | LopNrAdFar==.)

*Restrict sample
keep if inrange(FodAr, 1932, 1992)

*tempfile Sample
*save `Sample'

*Add grandparents
rename LopNr aLopNr
rename LopNrBioFar aLopNrBioFar
rename LopNrBioMor LopNr

merge m:1 LopNr using `BioForaldrar'
drop if _merge==2
drop _merge

rename LopNrBioMor LopNrBioMorMor
rename LopNrBioFar LopNrBioMorFar

rename LopNr aLopNrBioMor
rename aLopNrBioFar LopNr

merge m:1 LopNr using `BioForaldrar'
drop if _merge==2
drop _merge

rename LopNrBioMor LopNrBioFarMor
rename LopNrBioFar LopNrBioFarFar
rename LopNr LopNrBioFar
rename aLopNrBioMor LopNrBioMor

rename LopNrAdFar aLopNrAdFar
rename LopNrAdMor LopNr
rename LopNrBioMor aLopNrBioMor
rename LopNrBioFar aLopNrBioFar

merge m:1 LopNr using `BioForaldrar'
drop if _merge==2
drop _merge

rename LopNrBioMor LopNrAdMorMor
rename LopNrBioFar LopNrAdMorFar

rename LopNr aLopNrAdMor
rename aLopNrAdFar LopNr

merge m:1 LopNr using `BioForaldrar'
drop if _merge==2
drop _merge

rename LopNrBioMor LopNrAdFarMor
rename LopNrBioFar LopNrAdFarFar
rename LopNr LopNrAdFar
rename aLopNr LopNr
rename aLopNrBioMor LopNrBioMor
rename aLopNrBioFar LopNrBioFar
rename aLopNrAdMor LopNrAdMor

order LopNr LopNrBioMor LopNrBioFar LopNrAdMor LopNrAdFar LopNrBioMorMor LopNrBioMorFar LopNrBioFarMor LopNrBioFarFar LopNrAdMorMor LopNrAdMorFar LopNrAdFarMor LopNrAdFarFar

*Drop those who were adopted by grandparents in native adoption sample
replace AdoptionSv = . if LopNrAdMor==LopNrBioMorMor
replace AdoptionSv = . if LopNrAdMor==LopNrBioFarMor
replace AdoptionSv = . if LopNrAdFar==LopNrBioMorFar
replace AdoptionSv = . if LopNrAdFar==LopNrBioFarFar


*Add turnout and birth year to generation 2 and 3
foreach name in FodAr LopNr LopNrBioMor LopNrBioFar LopNrAdMor LopNrAdFar {
rename `name' a`name'
}

foreach name in BioMor BioFar AdMor AdFar {
rename aLopNr`name' LopNr
merge m:1 LopNr using `Fodelseuppg', keepusing(FodAr)
drop if _merge==2
drop _merge
rename FodAr `name'FodAr
rename LopNr aLopNr`name'
}


rename aFodAr FodAr
rename aLopNr LopNr
foreach num in 70 82 94R 94EU 09 10 {
merge 1:1 LopNr using `Rost`num''
drop if _merge==2
drop _merge
}
rename LopNr aLopNr
rename Rost70 aRost70
rename Rost82 aRost82
rename Rost94R aRost94R
rename Rost94EU aRost94EU
rename Rost09 aRost09
rename Rost10 aRost10

foreach name in BioMor BioFar AdMor AdFar {
rename aLopNr`name' LopNr
foreach num in 70 82 94R 94EU 09 10 {
merge m:1 LopNr using `Rost`num''
drop if _merge==2
drop _merge
rename Rost`num' `name'Rost`num'
}
rename LopNr aLopNr`name'
}



*Add years of education to generation 2
rename aLopNr LopNr
foreach year in 1970 1990 2000 2010 2014 {
merge 1:1 LopNr using `UtbAr`year''
drop if _merge==2
drop _merge
}
rename LopNr aLopNr
egen UtbAr = rowmax(UtbAr1970 UtbAr1990 UtbAr2000 UtbAr2010 UtbAr2014)
drop UtbAr1970 UtbAr1990 UtbAr2000 UtbAr2010 UtbAr2014


foreach name in BioMor BioFar AdMor AdFar {
rename aLopNr`name' LopNr
foreach year in 1970 1990 2000 2010 2014  {
merge m:1 LopNr using `UtbAr`year''
drop if _merge==2
drop _merge
rename UtbAr`year' `name'UtbAr`year'
}
rename LopNr aLopNr`name'
}
foreach name in BioMor BioFar AdMor AdFar {
egen `name'UtbAr = rowmax(`name'UtbAr1970 `name'UtbAr1990 `name'UtbAr2000 `name'UtbAr2010 `name'UtbAr2014)
drop `name'UtbAr1970 `name'UtbAr1990 `name'UtbAr2000 `name'UtbAr2010 `name'UtbAr2014
}

egen BioFUtbAr = rowmax(BioMorUtbAr BioFarUtbAr)
egen AdFUtbAr = rowmax(AdMorUtbAr AdFarUtbAr)




foreach name in LopNr LopNrBioMor LopNrBioFar LopNrAdMor LopNrAdFar Rost70 Rost82 Rost94R Rost94EU Rost09 Rost10 {
rename a`name' `name'
}


*Add parental SES
rename LopNr aLopNr
foreach name in BioMor  BioFar AdMor AdFar {
rename LopNr`name' LopNr
merge m:1 LopNr using `SEI'
drop if _merge==2
drop _merge
gen `name'SEI = SEI_joj1960
foreach year in 1970 1975 1980 1985 1990 {
replace `name'SEI = SEI_joj`year' if `name'SEI==.
}
drop SEI_joj*
gen `name'Klass = 0 if inrange(`name'SEI, 11, 21)
replace `name'Klass = 1 if inrange(`name'SEI, 36, 89)
rename LopNr LopNr`name'
}
rename aLopNr LopNr


*Use the dominance relation described in MIS 1982:4 to code household SEI
foreach name in BioMor  BioFar AdMor AdFar {	
	gen `name'DomOrd=.
	replace `name'DomOrd=1 if `name'SEI==60
	replace `name'DomOrd=2 if `name'SEI==56
	replace `name'DomOrd=3 if `name'SEI==89
	replace `name'DomOrd=4 if `name'SEI==79
	replace `name'DomOrd=5 if `name'SEI==46
	replace `name'DomOrd=6 if `name'SEI==36
	replace `name'DomOrd=7 if `name'SEI==21
	replace `name'DomOrd=8 if `name'SEI==11
}
	
	gen BioFSEI = BioFarSEI
	replace BioFSEI = BioMorSEI if BioMorDomOrd>BioFarDomOrd

	gen AdFSEI = AdFarSEI
	replace AdFSEI = AdMorSEI if AdMorDomOrd>AdFarDomOrd

	gen BioFKlass = BioFarKlass
	replace BioFKlass = BioMorKlass if BioFarKlass==.
	replace BioFKlass = BioMorKlass if BioFarKlass==0 & BioMorKlass!=.
	
	gen AdFKlass = AdFarKlass
	replace AdFKlass = AdMorKlass if AdFarKlass==.
	replace AdFKlass = AdMorKlass if AdFarKlass==0 & AdMorKlass!=.
	
	drop BioMorDomOrd BioFarDomOrd AdMorDomOrd AdFarDomOrd



*Generate aggregate turnout indicators for generation 2 across all elections
egen BioFRost = rowmean(BioMorRost70 BioFarRost70 BioMorRost82 BioFarRost82 BioMorRost94R BioFarRost94R BioMorRost94EU BioFarRost94EU BioMorRost10 BioFarRost10)
egen AdFRost = rowmean(AdMorRost70 AdFarRost70 AdMorRost82 AdFarRost82 AdMorRost94R AdFarRost94R AdMorRost94EU AdFarRost94EU AdMorRost10 AdFarRost10)

*Generate aggregate birth year indicators within generation 2 --> mean age rounded to nearest integer
egen BioFFodAr = rowmean(BioMorFodAr BioFarFodAr)
replace BioFFodAr = round(BioFFodAr)
egen AdFFodAr = rowmean(AdMorFodAr AdFarFodAr)
replace AdFFodAr = round(AdFFodAr)




*Append for each election
*Add aggregate turnout indicators for generation 2 and 3: i) up to and including
*the election and ii) all elections before the current election
*Add also aggregate turnout variable not incuding voting in the EU referendum
*Add indicator for living with at least one rearing parent
preserve
gen ValAr = 1
gen ValAlder = 1970-FodAr
replace ValAlder = . if ValAlder<0
rename Rost70 Rost
drop Rost82 Rost94R Rost94EU Rost10

gen BioMorRostCum = BioMorRost70
gen BioFarRostCum = BioFarRost70
gen AdMorRostCum = AdMorRost70
gen AdFarRostCum = AdFarRost70

gen BioMorRostCum1 = .
gen BioFarRostCum1 = .
gen AdMorRostCum1 = .
gen AdFarRostCum1 = .

gen BioFRostCum1 = .
gen AdFRostCum1 = .


foreach name in BioMor BioFar AdMor AdFar {
rename `name'Rost70 `name'Rost
}
*Generate indicator for spousal consistency in voting
gen BioFStabilVal = BioMorRost + BioFarRost
replace BioFStabilVal = 2 if BioFStabilVal==0
replace BioFStabilVal = BioFStabilVal-1
gen AdFStabilVal = AdMorRost + AdFarRost
replace AdFStabilVal = 2 if AdFStabilVal==0
replace AdFStabilVal = AdFStabilVal-1


rename LopNr aLopNr
foreach name in BioMor BioFar AdMor AdFar {
rename LopNr`name' LopNr
merge m:1 LopNr using `FamId1970'
drop if _merge==2
drop _merge
rename FamId1970 `name'FamId1970
rename LopNr LopNr`name'
}

rename aLopNr LopNr
merge 1:1 LopNr using `FamId1970'
drop if _merge==2
drop _merge

gen SammaFamId = 0
replace SammaFamId = 1 if FamId1970==BioMorFamId1970 & Adoption!=1
replace SammaFamId = 1 if FamId1970==BioFarFamId1970 & Adoption!=1
replace SammaFamId = 1 if FamId1970==AdMorFamId1970 & Adoption==1
replace SammaFamId = 1 if FamId1970==AdFarFamId1970 & Adoption==1
drop FamId1970 BioMorFamId1970 BioFarFamId1970 AdMorFamId1970 AdFarFamId1970


merge 1:1 LopNr using "E:\ProjData\AdoptionStudy\SameParish.dta", keepusing(SammaForsamling70 SammaForsamling70Andel SammaLan70)
drop if _merge==2 
drop _merge
rename SammaForsamling70 SammaForsamling
rename SammaForsamling70Andel SammaForsamlingAndel
rename SammaLan70 SammaLan

tempfile 1970
save `1970'
restore


preserve
gen ValAr = 2
gen ValAlder = 1982-FodAr
replace ValAlder = . if ValAlder<0
rename Rost82 Rost
drop Rost70 Rost94R Rost94EU Rost10

egen BioMorRostCum = rowmean(BioMorRost70 BioMorRost82)
egen BioFarRostCum = rowmean(BioFarRost70 BioFarRost82)
egen AdMorRostCum = rowmean(AdMorRost70 AdMorRost82)
egen AdFarRostCum = rowmean(AdFarRost70 AdFarRost82)

gen BioMorRostCum1 = BioMorRost70
gen BioFarRostCum1 = BioFarRost70
gen AdMorRostCum1 = AdMorRost70
gen AdFarRostCum1 = AdFarRost70

egen BioFRostCum1 = rowmean(BioMorRost70 BioFarRost70)
egen AdFRostCum1 = rowmean(AdMorRost70 AdFarRost70)


foreach name in BioMor BioFar AdMor AdFar {
rename `name'Rost82 `name'Rost
}
*Generate indicator for spousal consistency in voting
gen BioFStabilVal = BioMorRost + BioFarRost
replace BioFStabilVal = 2 if BioFStabilVal==0
replace BioFStabilVal = BioFStabilVal-1
gen AdFStabilVal = AdMorRost + AdFarRost
replace AdFStabilVal = 2 if AdFStabilVal==0
replace AdFStabilVal = AdFStabilVal-1


rename LopNr aLopNr
foreach name in BioMor BioFar AdMor AdFar {
rename LopNr`name' LopNr
foreach year in 1980 1985 {
merge m:1 LopNr using `FamId`year''
drop if _merge==2
drop _merge
rename FamId`year' `name'FamId`year'
}
rename LopNr LopNr`name'
}

rename aLopNr LopNr
foreach year in 1980 1985 {
merge 1:1 LopNr using `FamId`year''
drop if _merge==2
drop _merge
}

gen SammaFamId = 0
replace SammaFamId = 1 if FamId1980==BioMorFamId1980 & Adoption!=1
replace SammaFamId = 1 if FamId1980==BioFarFamId1980 & Adoption!=1
replace SammaFamId = 1 if FamId1980==AdMorFamId1980 & Adoption==1
replace SammaFamId = 1 if FamId1980==AdFarFamId1980 & Adoption==1

replace SammaFamId = 0 if FamId1985!=BioMorFamId1985 & Adoption!=1 & inrange(ValAlder, 17, 20)
replace SammaFamId = 0 if FamId1985!=BioFarFamId1985 & Adoption!=1 & inrange(ValAlder, 17, 20)
replace SammaFamId = 0 if FamId1985!=AdMorFamId1985 & Adoption==1 & inrange(ValAlder, 17, 20)
replace SammaFamId = 0 if FamId1985!=AdFarFamId1985 & Adoption==1 & inrange(ValAlder, 17, 20)
drop FamId1980 BioMorFamId1980 BioFarFamId1980 AdMorFamId1980 AdFarFamId1980 FamId1985 BioMorFamId1985 BioFarFamId1985 AdMorFamId1985 AdFarFamId1985

merge 1:1 LopNr using "E:\ProjData\AdoptionStudy\SameParish.dta", keepusing(SammaForsamling82 SammaForsamling82Andel SammaLan82)
drop if _merge==2 
drop _merge
rename SammaForsamling82 SammaForsamling
rename SammaForsamling82Andel SammaForsamlingAndel
rename SammaLan82 SammaLan

tempfile 1982
save `1982'
restore


preserve
gen ValAr = 3
gen ValAlder = 1994-FodAr
replace ValAlder = . if ValAlder<0
rename Rost94R Rost
drop Rost70 Rost82 Rost94EU Rost10

egen BioMorRostCum = rowmean(BioMorRost70 BioMorRost82 BioMorRost94R)
egen BioFarRostCum = rowmean(BioFarRost70 BioFarRost82 BioFarRost94R)
egen AdMorRostCum = rowmean(AdMorRost70 AdMorRost82 AdMorRost94R)
egen AdFarRostCum = rowmean(AdFarRost70 AdFarRost82 AdFarRost94R)

egen BioMorRostCum1 = rowmean(BioMorRost70 BioMorRost82)
egen BioFarRostCum1 = rowmean(BioFarRost70 BioFarRost82)
egen AdMorRostCum1 = rowmean(AdMorRost70 AdMorRost82)
egen AdFarRostCum1 = rowmean(AdFarRost70 AdFarRost82)

egen BioFRostCum1 = rowmean(BioMorRost70 BioFarRost70 BioMorRost82 BioFarRost82)
egen AdFRostCum1 = rowmean(AdMorRost70 AdFarRost70 AdMorRost82 AdFarRost82)


foreach name in BioMor BioFar AdMor AdFar {
rename `name'Rost94R `name'Rost
}
*Generate indicator for spousal consistency in voting
gen BioFStabilVal = BioMorRost + BioFarRost
replace BioFStabilVal = 2 if BioFStabilVal==0
replace BioFStabilVal = BioFStabilVal-1
gen AdFStabilVal = AdMorRost + AdFarRost
replace AdFStabilVal = 2 if AdFStabilVal==0
replace AdFStabilVal = AdFStabilVal-1


rename LopNr aLopNr
foreach name in BioMor BioFar AdMor AdFar {
rename LopNr`name' LopNr
merge m:1 LopNr using `FamId1994'
drop if _merge==2
drop _merge
rename FamId1994 `name'FamId1994
rename LopNr LopNr`name'
}

rename aLopNr LopNr
merge 1:1 LopNr using `FamId1994'
drop if _merge==2
drop _merge

gen SammaFamId = 0
replace SammaFamId = 1 if FamId1994==BioMorFamId1994 & Adoption!=1
replace SammaFamId = 1 if FamId1994==BioFarFamId1994 & Adoption!=1
replace SammaFamId = 1 if FamId1994==AdMorFamId1994 & Adoption==1
replace SammaFamId = 1 if FamId1994==AdFarFamId1994 & Adoption==1
drop FamId1994 BioMorFamId1994 BioFarFamId1994 AdMorFamId1994 AdFarFamId1994


merge 1:1 LopNr using "E:\ProjData\AdoptionStudy\SameParish.dta", keepusing(SammaForsamling94 SammaForsamling94Andel SammaLan94)
drop if _merge==2 
drop _merge
rename SammaForsamling94 SammaForsamling
rename SammaForsamling94Andel SammaForsamlingAndel
rename SammaLan94 SammaLan

tempfile 1994R
save `1994R'
restore


preserve
gen ValAr = 4
gen ValAlder = 1994-FodAr
replace ValAlder = . if ValAlder<0
rename Rost94EU Rost
drop Rost70 Rost82 Rost94R Rost10

egen BioMorRostCum = rowmean(BioMorRost70 BioMorRost82 BioMorRost94R BioMorRost94EU)
egen BioFarRostCum = rowmean(BioFarRost70 BioFarRost82 BioFarRost94R BioFarRost94EU)
egen AdMorRostCum = rowmean(AdMorRost70 AdMorRost82 AdMorRost94R AdMorRost94EU)
egen AdFarRostCum = rowmean(AdFarRost70 AdFarRost82 AdFarRost94R AdFarRost94EU)

egen BioMorRostCum1 = rowmean(BioMorRost70 BioMorRost82 BioMorRost94R)
egen BioFarRostCum1 = rowmean(BioFarRost70 BioFarRost82 BioFarRost94R)
egen AdMorRostCum1 = rowmean(AdMorRost70 AdMorRost82 AdMorRost94R)
egen AdFarRostCum1 = rowmean(AdFarRost70 AdFarRost82 AdFarRost94R)

egen BioMorRostCum17082 = rowmean(BioMorRost70 BioMorRost82)
egen BioFarRostCum17082 = rowmean(BioFarRost70 BioFarRost82)
egen AdMorRostCum17082 = rowmean(AdMorRost70 AdMorRost82)
egen AdFarRostCum17082 = rowmean(AdFarRost70 AdFarRost82)

egen BioFRostCum1 = rowmean(BioMorRost70 BioFarRost70 BioMorRost82 BioFarRost82 BioMorRost94R BioFarRost94R)
egen AdFRostCum1 = rowmean(AdMorRost70 AdFarRost70 AdMorRost82 AdFarRost82 AdMorRost94R AdFarRost94R)

egen BioFRostCum17082 = rowmean(BioMorRost70 BioFarRost70 BioMorRost82 BioFarRost82)
egen AdFRostCum17082 = rowmean(AdMorRost70 AdFarRost70 AdMorRost82 AdFarRost82)


foreach name in BioMor BioFar AdMor AdFar {
rename `name'Rost94EU `name'Rost
}
*Generate indicator for spousal consistency in voting
gen BioFStabilVal = BioMorRost + BioFarRost
replace BioFStabilVal = 2 if BioFStabilVal==0
replace BioFStabilVal = BioFStabilVal-1
gen AdFStabilVal = AdMorRost + AdFarRost
replace AdFStabilVal = 2 if AdFStabilVal==0
replace AdFStabilVal = AdFStabilVal-1


rename LopNr aLopNr
foreach name in BioMor BioFar AdMor AdFar {
rename LopNr`name' LopNr
merge m:1 LopNr using `FamId1994'
drop if _merge==2
drop _merge
rename FamId1994 `name'FamId1994
rename LopNr LopNr`name'
}

rename aLopNr LopNr
merge 1:1 LopNr using `FamId1994'
drop if _merge==2
drop _merge

gen SammaFamId = 0
replace SammaFamId = 1 if FamId1994==BioMorFamId1994 & Adoption!=1
replace SammaFamId = 1 if FamId1994==BioFarFamId1994 & Adoption!=1
replace SammaFamId = 1 if FamId1994==AdMorFamId1994 & Adoption==1
replace SammaFamId = 1 if FamId1994==AdFarFamId1994 & Adoption==1
drop FamId1994 BioMorFamId1994 BioFarFamId1994 AdMorFamId1994 AdFarFamId1994


merge 1:1 LopNr using "E:\ProjData\AdoptionStudy\SameParish.dta", keepusing(SammaForsamling94 SammaForsamling94Andel SammaLan94)
drop if _merge==2 
drop _merge
rename SammaForsamling94 SammaForsamling
rename SammaForsamling94Andel SammaForsamlingAndel
rename SammaLan94 SammaLan

tempfile 1994EU
save `1994EU'
restore


preserve
gen ValAr = 5
gen ValAlder = 2010-FodAr
replace ValAlder = . if ValAlder<0
rename Rost10 Rost
drop Rost70 Rost82 Rost94R Rost94EU

egen BioMorRostCum = rowmean(BioMorRost70 BioMorRost82 BioMorRost94R BioMorRost10)
egen BioFarRostCum = rowmean(BioFarRost70 BioFarRost82 BioFarRost94R BioFarRost10)
egen AdMorRostCum = rowmean(AdMorRost70 AdMorRost82 AdMorRost94R AdMorRost10)
egen AdFarRostCum = rowmean(AdFarRost70 AdFarRost82 AdFarRost94R AdFarRost10)

egen BioMorRostCum1 = rowmean(BioMorRost70 BioMorRost82 BioMorRost94R)
egen BioFarRostCum1 = rowmean(BioFarRost70 BioFarRost82 BioFarRost94R)
egen AdMorRostCum1 = rowmean(AdMorRost70 AdMorRost82 AdMorRost94R)
egen AdFarRostCum1 = rowmean(AdFarRost70 AdFarRost82 AdFarRost94R)

egen BioFRostCum1 = rowmean(BioMorRost70 BioFarRost70 BioMorRost82 BioFarRost82 BioMorRost94R BioFarRost94R)
egen AdFRostCum1 = rowmean(AdMorRost70 AdFarRost70 AdMorRost82 AdFarRost82 AdMorRost94R AdFarRost94R)


foreach name in BioMor BioFar AdMor AdFar {
rename `name'Rost10 `name'Rost
}
*Generate indicator for spousal consistency in voting
gen BioFStabilVal = BioMorRost + BioFarRost
replace BioFStabilVal = 2 if BioFStabilVal==0
replace BioFStabilVal = BioFStabilVal-1
gen AdFStabilVal = AdMorRost + AdFarRost
replace AdFStabilVal = 2 if AdFStabilVal==0
replace AdFStabilVal = AdFStabilVal-1


rename LopNr aLopNr
foreach name in BioMor BioFar AdMor AdFar {
rename LopNr`name' LopNr
merge m:1 LopNr using `FamId2010'
drop if _merge==2
drop _merge
rename FamId2010 `name'FamId2010
rename LopNr LopNr`name'
}

rename aLopNr LopNr
merge 1:1 LopNr using `FamId2010'
drop if _merge==2
drop _merge

gen SammaFamId = 0
replace SammaFamId = 1 if FamId2010==BioMorFamId2010 & Adoption!=1
replace SammaFamId = 1 if FamId2010==BioFarFamId2010 & Adoption!=1
replace SammaFamId = 1 if FamId2010==AdMorFamId2010 & Adoption==1
replace SammaFamId = 1 if FamId2010==AdFarFamId2010 & Adoption==1
drop FamId2010 BioMorFamId2010 BioFarFamId2010 AdMorFamId2010 AdFarFamId2010


merge 1:1 LopNr using "E:\ProjData\AdoptionStudy\SameParish.dta", keepusing(SammaForsamling10 SammaForsamling10Andel SammaLan10)
drop if _merge==2
drop _merge
rename SammaForsamling10 SammaForsamling
rename SammaForsamling10Andel SammaForsamlingAndel
rename SammaLan10 SammaLan

tempfile 2010
save `2010'
restore


*Append all files
use `1970', clear
foreach name in 1982 1994R 1994EU 2010 {
append using ``name''
}



***Retain parental voting in each election year
foreach name in BioMor BioFar AdMor AdFar {
replace `name'Rost70 = `name'Rost if ValAr==1
replace `name'Rost82 = `name'Rost if ValAr==2
replace `name'Rost94R = `name'Rost if ValAr==3
replace `name'Rost94EU = `name'Rost if ValAr==4
replace `name'Rost10 = `name'Rost if ValAr==5
}



***Generate some additional variables***
*Indicator for having parental turnout info from at least 3 elections
foreach name in BioMor BioFar AdMor AdFar {
foreach num in Rost70 Rost82 Rost94R Rost10 {
gen occ`name'`num' = 1 if `name'`num'!=.
}
}
foreach name in BioMor BioFar AdMor AdFar {
egen occ`name'Rost = rowtotal(occ`name'Rost70 occ`name'Rost82 occ`name'Rost94R occ`name'Rost10)
tab occ`name'Rost
replace occ`name'Rost = . if inrange(occ`name'Rost, 0, 1)
}
egen BioFAlla = rowmean(occBioMorRost occBioFarRost)
replace BioFAlla = 1 if BioFAlla!=.
egen AdFAlla = rowmean(occAdMorRost occAdFarRost)
replace AdFAlla = 1 if AdFAlla!=.
drop occ*



*2 indicators for stability in parental voting
*Variation in stability is due both to over time variation within parent
*and across parent variation at a specific time point
*2nd indicator: Herfindahl's index standardized to 0-1 range
egen BioFStabil = rowmean(BioMorRost70 BioFarRost70 BioMorRost82 BioFarRost82 BioMorRost94R BioFarRost94R BioMorRost10 BioFarRost10)
gen occ1 = BioFStabil
replace BioFStabil = 2*abs((BioFStabil-.5))
replace BioFStabil = 0 if BioFStabil!=1 & BioFStabil!=.
gen BioFStabilHI = -1*(1-(occ1^2+(1-occ1)^2))/0.5+1


egen AdFStabil = rowmean(AdMorRost70 AdFarRost70 AdMorRost82 AdFarRost82 AdMorRost94R AdFarRost94R AdMorRost10 AdFarRost10)
gen occ2 = AdFStabil
replace AdFStabil = 2*abs((AdFStabil-.5))
replace AdFStabil = 0 if AdFStabil!=1 & AdFStabil!=.
gen AdFStabilHI = -1*(1-(occ2^2+(1-occ2)^2))/0.5+1

drop occ*


replace ValAlder = ValAlder-18
replace ValAlder =. if ValAlder<0
gen ValAlder01 = ValAlder/32
gen lnValAlder = ln(ValAlder+1)


foreach name in BioMor BioFar AdMor AdFar {
*foreach level in 70 82 94R 94EU 09 10 {
foreach level in 09 {
drop `name'Rost`level'
}
}


*Generate variables needed for the combined ownbirth + foreign adoptees analysis
gen FRostCum1 = BioFRostCum1 if Adoption!=1
replace FRostCum1 = AdFRostCum1 if AdoptionUtl==1
gen FarRostCum1 = BioFarRostCum1 if Adoption!=1
replace FarRostCum1 = AdFarRostCum1 if AdoptionUtl==1
gen MorRostCum1 = BioMorRostCum1 if Adoption!=1
replace MorRostCum1 = AdMorRostCum1 if AdoptionUtl==1

gen FFodAr = BioFFodAr if Adoption!=1
replace FFodAr = AdFFodAr if AdoptionUtl==1
gen FarFodAr = BioFarFodAr if Adoption!=1
replace FarFodAr = AdFarFodAr if AdoptionUtl==1
gen MorFodAr = BioMorFodAr if Adoption!=1
replace MorFodAr = AdMorFodAr if AdoptionUtl==1

gen FUtbAr = BioFUtbAr if Adoption!=1
replace FUtbAr = AdFUtbAr if AdoptionUtl==1
gen FarUtbAr = BioFarUtbAr if Adoption!=1
replace FarUtbAr = AdFarUtbAr if AdoptionUtl==1
gen MorUtbAr = BioMorUtbAr if Adoption!=1
replace MorUtbAr = AdMorUtbAr if AdoptionUtl==1

gen FStabil = BioFStabil if Adoption!=1
replace FStabil = AdFStabil if AdoptionUtl==1

gen FStabilHI = BioFStabilHI if Adoption!=1
replace FStabilHI = AdFStabilHI if AdoptionUtl==1

gen FAlla = BioFAlla
replace FAlla = AdFAlla if AdoptionUtl==1

gen LopNrMor = LopNrBioMor
replace LopNrMor = LopNrAdMor if AdoptionUtl==1

gen EjAdoption = 0 if Adoption==1
replace EjAdoption = 1 if EjAdoption==.



save "E:\ProjData\AdoptionStudy/SampleStacked.dta", replace

